package com.boodaba.todocarts.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.boodaba.todocarts.R;
import com.boodaba.todocarts.app.ITodoSettingsActivity;
import com.boodaba.todocarts.app.TodoSettingsPresenter;
import com.google.inject.Inject;

import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import roboguice.activity.GuiceActivity;
import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;

public class TodoSettingsActivity extends GuiceActivity implements ITodoSettingsActivity {
	
	@Inject protected TodoSettingsPresenter mPresenter;
	@InjectView(android.R.id.text1) protected EditText mInputString;
	@InjectView(android.R.id.button1) protected ImageButton mInputButton;
	@InjectView(android.R.id.list) protected ListView mListBox;
	@InjectExtra(value="labelTitle") protected String mTargetLabelTitle;

	private ItemsAdapter mAdapterForList;
	
	private Map<String, Boolean> mWorkingMap;
	private String mSelectedString;
	//private int mSelectedPosition;
	//private View mSelectedStringView;
	private boolean mFreezeFilterFlag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_todo_settings);
		mPresenter.initializeWith(this);
		
		clearSelection();
		
		mInputString.addTextChangedListener(new ItWatches());
		
		mInputButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				clickButton();
			}
		});
		
		mListBox.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(
					AdapterView<?> parent,
					View view,
					int position,
					long id) {
				if (mSelectedString == null) {
					mSelectedString = mAdapterForList.getItem(position);
					//mSelectedPosition = position;
					//mSelectedStringView = view;
					mPresenter.checkItem();
					return;
				}
				clearSelection();
			}
		});
		
		mListBox.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(
					AdapterView<?> parent,
					View view,
					int position,
					long id) {
				
				mSelectedString = mAdapterForList.getItem(position);
				//mSelectedPosition = position;
				//mSelectedStringView = view;
				setSelection();
				return true;
			}
		});
	}
	
	void clickButton() {
		mPresenter.submitInput();
	}

	public String getTargetLabelTitle() {
		return mTargetLabelTitle;
	}

	public void setWorkingTitle(String activityTitle) {
		setTitle(activityTitle);
	}

	public void setWorkingList(Map<String, Boolean> workingMap) {
		mWorkingMap = workingMap;
	}

	public String getInputString() {
		return mInputString.getText().toString();
	}
	
	public String getSelected() {
		return mSelectedString;
	}

	public void addToWorkingList(String newItem) {
		mWorkingMap.put(newItem, false);
		clearSelection();
	}
	
	public void replaceInWorkingList(String updtItem) {
		mWorkingMap.remove(mSelectedString);
		mWorkingMap.put(updtItem, false);
		
		clearSelection();
	}

	public void removeFromWorkingList() {
		mWorkingMap.remove(mSelectedString);

		clearSelection();
	}
	
	public void chargeInWorkingList() {
		mWorkingMap.put(mSelectedString, false);
		//markItemView();
		clearSelection();
	}

	public void dischargeInWorkingList() {
		mWorkingMap.put(mSelectedString, true);
		//markItemView();
		clearSelection();
	}

	void setSelection() {
		mFreezeFilterFlag = true;
		mInputString.setText(mSelectedString);
		mInputString.setSelection(mSelectedString.length());
		mInputString.setHint(mSelectedString);
		mInputButton.setImageResource(android.R.drawable.ic_menu_edit);
	}
	
	void clearSelection() {
		mFreezeFilterFlag = true;
		mSelectedString = null;
		//mSelectedStringView = null;
		mInputString.setText("");
		mInputString.setHint(R.string.new_todo_hint);
		
		List<String> titleList = new ArrayList<String>();
		Collections.addAll(titleList, mWorkingMap.keySet().toArray(new String[] {}));
		Collections.sort(titleList);
		
		mAdapterForList = new ItemsAdapter(
				this,
				R.layout.list_item_simple,
				titleList);
		mListBox.setAdapter(mAdapterForList);
		
		mFreezeFilterFlag = false;
		mInputButton.setImageResource(android.R.drawable.ic_menu_add);
	}
	
//	private void markItemView() {
//		markItemView((TextView )mSelectedStringView, mSelectedString);
//	}
	
	private void markItemView(TextView tv, String selectedString) {
		boolean notStriked = (tv.getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) == 0;
		boolean chargeFlag = mWorkingMap.get(selectedString);

		if (chargeFlag && notStriked) {
			tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
			return;
		}
		if (!(chargeFlag || notStriked)) {
			tv.setPaintFlags(tv.getPaintFlags() ^ Paint.STRIKE_THRU_TEXT_FLAG);
		}
	}
	
	private class ItemsAdapter extends ArrayAdapter<String> {
		
		private int mViewResId;
		
		public ItemsAdapter(Context context, int textViewResourceId, List<String> objects) {
			super(context, textViewResourceId, objects);
			mViewResId = textViewResourceId;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			
			if (v == null) {
				v = getLayoutInflater().inflate(mViewResId, null);
			}
			
			TextView selectedStringView = (TextView )v.findViewById(R.id.todo_title);
			String selectedString = getItem(position);
			selectedStringView.setText(selectedString);
			markItemView(selectedStringView, selectedString);
			return v;
		}
	}
	
	private class ItWatches implements TextWatcher {

		public void afterTextChanged(Editable s) {
			final String inStr = s.toString().trim();
			
			boolean newMode = (mSelectedString == null);
			boolean emptyIn = (inStr.length() == 0);
						
			if (!newMode && emptyIn) {
				mInputButton.setImageResource(android.R.drawable.ic_menu_delete);
			}
			
			if (!(newMode  || emptyIn)) {
				mInputButton.setImageResource(android.R.drawable.ic_menu_edit);
			}
			
			
			if (mFreezeFilterFlag) {
				return;
			}
			mAdapterForList.getFilter().filter(inStr);
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			return;
		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			return;
		}
		
	}
}
